<?php

namespace app\admin\controller;

use think\db\Where;
// use think;
use app\admin\model\User;
use think\response\Jump;

class Admin extends Jump
{
    //登录
    public function index()
    {
        if (!empty($_POST)) {
            $passcode = $_POST['passcode'];
            // session_start();
            $captcha = new Captcha();
            if ($captcha->check($passcode)) {
                // //获取的用户名和密码
                $username = $_POST['username'];
                $password = $_POST['password'];
                $cond = array(
                    'user_name'  =>  $username,
                    'user_pwd'   => md5(md5($password))
                );
                $data = \app\admin\model\User::Where($cond)->select();
                if (count($data)) {

                    //记录用户名和密码
                    if (isset($_POST['remember'])) {
                        $time = time() + 3600 * 24 * 7;
                        setcookie('name', $_POST['username'], $time);
                        setcookie('pwd', $_POST['password'], $time);
                    }

                    $_SESSION['username'] = $username;
                    $this->wdsuccess('http://127.0.0.1/bnstudy/public/index.php/admin/cate/index', '登录成功');
                } else {
                    $this->wderror('http://127.0.0.1/bnstudy/public/index.php/admin/Admin/index', '用户名或密码错误');
                }
            } else {
                $this->wderror('http://127.0.0.1/bnstudy/public/index.php/admin/Admin/index', '验证码错误');
            }
        }
        //第一步：显示登陆界面
        $name = $_COOKIE['name'] ?? '';
        $pwd = $_COOKIE['pwd'] ?? '';
        return view('login');
    }
    // 注册
    public function register()
    {
        if (!empty($_POST)) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $passcode = $_POST['passcode'];
            $captcha = new Captcha();
            if ($password != '' & $username != '') {
                // dump('可以注册');

                if ($captcha->check($passcode)) {
                    //获取的用户名和密码 
                    $username = $_POST['username'];
                    $password = $_POST['password'];
                    $cond = array(
                        'user_name'  =>  $username,
                        'user_pwd'   => md5(md5($password))
                    );
                    $aa = new \app\admin\model\User();
                    $aa->save($cond);
                    $this->wdsuccess('http://127.0.0.1/bnstudy/public/index.php/admin/Admin/index', '注册成功，请登录');
                } else {
                    $this->wderror('http://127.0.0.1/bnstudy/public/index.php/admin/Admin/register', '验证码错误，请重新注册');
                }
            } else {
                $this->wderror('http://127.0.0.1/bnstudy/public/index.php/admin/Admin/register', '密码或用户名不能为空');
            }
        }
        function yzm()
        {
        }
        return view('register');
    }
    //验证用户名是否存在
    public function checkUserAction()
    {
        function a($a)
        {
            $data = \app\admin\model\User::Where('user_name', $a)->select();
            return count($data);
        }
        echo a($_GET['username']);
    }
    //验证码
    public function verifyAction()
    {
        $captcha = new Captcha();
        $captcha->entry();
    }
    //安全退出
    public function logoutAction()
    {
        session_start();
        session_destroy();
        header('location:http://127.0.0.1/bnstudy/public/index.php/admin/Admin/index');
    }
}
